<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Preferences</title>
<script type="text/javascript">
function updateFiltersAndCss(){
	var port = chrome.extension.connect({name: 'adblock'});
	port.postMessage({command: 'UpdateFilters'});
	port.postMessage({command: 'UpdateCss'});
};

var list = {
	prefix: 'adblock+',
	set: function(txt){window.localStorage[this.prefix] = txt; updateFiltersAndCss()},
	get: function(){return window.localStorage[this.prefix] || ''},
	exist: function(){return !!window.localStorage[this.prefix]}
};

var userlist = {
	prefix: 'userfilters',
	set: function(txt){window.localStorage[this.prefix] = txt; updateFiltersAndCss()},
	get: function(){return window.localStorage[this.prefix] || ''},
	exist: function(){return !!window.localStorage[this.prefix]}
};

var subscriptionUrl = {
	prefix: 'subscription',
	set: function(url){window.localStorage[this.prefix] = url},
	get: function(){return window.localStorage[this.prefix] || ''},
	exist: function(){return !!window.localStorage[this.prefix]}
};

var convert = {
	prefix: 'convert',
	set: function(checked){window.localStorage[this.prefix] = checked ? 'checked' : ''},
	need: function(){return !!window.localStorage[this.prefix]},
	update: function(){document.getElementById('convertrules').checked = this.need()}
};

// interface
function showLoading(show){
	var img, ele = document.getElementById('subscribe');
	if(show){
		img = document.createElement('img');
		img.src = '';
		img.setAttribute('style', 'float: right; vertical-align: middle; ')
		ele.parentNode.appendChild(img);
	}
	else{
		img = ele.parentNode.lastElementChild;
		if(img && img instanceof HTMLImageElement)ele.parentNode.removeChild(img);
	}
};

function showAbout(){
	var s = document.getElementById('about').style;
	var m = document.getElementsByClassName('menu');
	if(s.display == 'none'){
		s.display = 'block';
		m[m.length - 2].style.display = 'none';
		m[m.length - 1].style.display = 'none';

	}
	else{
		s.display = 'none';
		m[m.length - 2].style.display = 'block';
		m[m.length - 1].style.display = 'block';
	}
};

function customUrl(ele){
	var prefix = 'customUrl';
	if(ele){
		window.localStorage[prefix] = ele.value;
	}
	else{
		var url = window.localStorage[prefix];
		if(url || url == '')document.getElementById('custom').value = url;
	}
};

function radioButton(ele){
	var e, f, url;
	if(ele){
		e = ele.nextElementSibling;
		if(e)subscriptionUrl.set(e.href || e.value || '');
	}
	else{
		f = document.getElementById('subscription').getElementsByTagName('input');
		url = subscriptionUrl.get();
		for(var i = 0; i < f.length; i++){
			f[i].checked = (f[i].type == 'radio' && (e = f[i].nextElementSibling) && (url == (e.href || e.value)));
		}
	}
};

function updateTextarea(){
	document.getElementById('rules').value = list.get();
};

function updateUserTextarea(){
	document.getElementById('userrules').value = userlist.get();
};

function clearSubscription(){
	if(subscriptionUrl.exist()){
		list.set('');
		subscriptionUrl.set('');
		updateTextarea();
		radioButton();
	}
};

// download the adblock plus list
function convertBlockingRule(rule){
	while(rule.indexOf('|') == 0){rule = rule.slice(1)};
	var options = null, sep = rule.indexOf('$');
	if(sep > 0){
		options = rule.slice(sep + 1);
		if(options != 'third-party')return '';
		rule = rule.slice(0, sep);
	};
	rule = rule.replace(/\^/g, '/');

	var rez = options ? '~' + rule.slice(0, rule.indexOf('/')) + '##*' : '##*';
	var a = rule.split('*');
	for(var i = 0; i < a.length; i++){
		if(a[i])rez += '[src*="' + a[i] + '"]';
	};
	return rez;
};

function downloadList(){
	var url = subscriptionUrl.get(), isNeedConvert = convert.need();
	if(!url)return;
	showLoading(true);
	var xhr = new XMLHttpRequest();
	var abortTimerId = window.setTimeout(function(){xhr.abort()}, 20000);
	var stopTimer = function(){window.clearTimeout(abortTimerId); showLoading(false)};
	try{
		xhr.onreadystatechange = function(){
			var a, txt, rule, rez = [], reTrim = /^\s+|\s+$/g;
			if (xhr.readyState == 4){
				stopTimer();
				txt = xhr.responseText;
				if(txt && txt.length > 10000){
					a = txt.split('\n');
					for(var i = 0; i < a.length; i++){
						rule = a[i].replace(reTrim, '');
						if(rule && rule[0] != '!' && rule[0] != '[' && rule[0] != '@' && !(rule[0] == '/' && rule[rule.length - 1] == '/')){
							if(rule.indexOf('#') != -1){
								rez.push(rule);
							}
							else if(isNeedConvert){
								rule = convertBlockingRule(rule);
								if(rule)rez.push(rule);
							}
						}
					};
					list.set(rez.join('\n'));
					updateTextarea();
				}
			  }
			};
		xhr.onerror = function(error){
			console.log('error: ' + error);
			stopTimer();
		};
		xhr.open('GET', url, true);
		xhr.send(null);
		}
	catch(e){
		console.log('exception: ' + e);
		stopTimer();
	};
};
</script>
<style type="text/css">
	body{ background-color: #EEEEEE; }
	textarea{ width: 100%; height: 20%; margin-top: 7px; }
	label{ display: block; padding: 4px; }
	label.checkbox{ display: inline; vertical-align: middle; }
	a{ color: #729FCF; }
	input[type="text"]{ width: 70%; margin: 2px; }
	#imghelp{ float:right; }
	#imghelp:hover{ outline: 1px outset #BBB; }
	h4.filters{ display: inline; }
	.buttons{ padding: 12px 2px 0; }
	.menu{
		width: 700px;
		background: #E6E6E6;
		padding: 7px;
		margin-left: auto;
		margin-right: auto;
		-webkit-border-radius: 7px;
		-webkit-box-shadow: 0px 0px 5px #8EC2EB;
	}
</style>
</head>
<body onload="updateTextarea();updateUserTextarea();customUrl();radioButton();convert.update();">
<div class="menu" id="subscription">
	<img id="imghelp" onclick="showAbout()" src="">
	<h4>Subscription:</h4>
	<label><input type="radio" name="subs" onclick="radioButton(this)">EasyList (USA): <a href="https://easylist.adblockplus.org/easylist.txt" target="_blank">https://easylist.adblockplus.org/easylist.txt</a></label>
	<label><input type="radio" name="subs" onclick="radioButton(this)">EasyList Germany (Deutschland) + EasyList: <a href="https://easylist.adblockplus.org/ares+easylist.txt" target="_blank">https://easylist.adblockplus.org/ares+easylist.txt</a></label>
	<label><input type="radio" name="subs" onclick="radioButton(this)">Liste FR (France) + EasyList: <a href="https://easylist.adblockplus.org/liste_fr+easylist.txt" target="_blank">https://easylist.adblockplus.org/liste_fr+easylist.txt</a></label>
	<label><input type="radio" name="subs" onclick="radioButton(this)">Morpeh Rus List + EasyList: <a href="https://easylist.adblockplus.org/morpeh+easylist.txt" target="_blank">https://easylist.adblockplus.org/morpeh+easylist.txt</a></label>
	<label><input type="radio" name="subs" onclick="radioButton(this)">RuAdList (Russia): <a href="http://ruadlist.googlecode.com/svn/trunk/adblock.txt" target="_blank">http://ruadlist.googlecode.com/svn/trunk/adblock.txt</a></label>
	<label><input type="radio" name="subs" onclick="radioButton(this)"><input type="text" value="https://easylist.adblockplus.org/" id="custom" onkeyup="customUrl(this);subscriptionUrl.set(this.value)"></label>
	<div class="buttons">
	<input type="button" id="subscribe" value="Subscribe" onclick="downloadList()">
	<input type="button" value="Cancel" onclick="clearSubscription()">
	<label class="checkbox"><input type="checkbox" id="convertrules" onclick="convert.set(this.checked)">use blocking rules</label>
	</div>
</div>
<br>
<div class="menu" id="about" style="display:none;">
	<h4>About:</h4>
	AdBlock+ ver. 1.1.9. An adblock extension for Google Chrome.
	<br>Links: 
	<a href="http://ruzanow.ru/0-4#chrome" target="_blank">site</a>, 
	<a href="mailto:ruzanow@gmail.com">e-mail</a>, 
	<a href="http://userscripts.org/scripts/show/46974" target="_blank">userscript version</a>, 
	<a href="http://www.chromeplugins.org/google/chrome-plugins/adblock-7523.html" target="_blank">forum</a>.
	<br><br>
	<span style="font-size:smaller;">This program is released under GPL General Public License. This program is free software; 
	you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
	either version 2 of the License, or (at your option) any later version.</span>
</div>

<div class="menu">
	<h4 class="filters">Global filters:</h4>
	<textarea id="rules"></textarea>
	<div class="buttons">
	<input type="button" value="Save" onclick="list.set(document.getElementById('rules').value);">
	<input type="button" value="Cancel" onclick="updateTextarea()">
	</div>
</div>
<br>
<div class="menu">
	<h4 class="filters">User filters:</h4>
	<textarea id="userrules"></textarea>
	<div class="buttons">
	<input type="button" value="Save" onclick="userlist.set(document.getElementById('userrules').value);">
	<input type="button" value="Cancel" onclick="updateUserTextarea()">
	<input type="button" value="Close" onclick="window.close()" style="float:right;">
	</div>
</div>
<br>
</body>
</html>
